package com.xinqi.xinqidev.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xinqi.xinqidev.pojo.vo.UserVo;
import com.xinqi.xinqidev.pojo.result.MessageResult;
import com.xinqi.xinqidev.pojo.entity.User;
import com.xinqi.xinqidev.pojo.result.Result;
import com.xinqi.xinqidev.pojo.result.ResultUtil;
import com.xinqi.xinqidev.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;


@Slf4j
@RestController
@RequestMapping("user")
public class UserController {


    @Autowired
    private UserService userService;



    /**
     * 登陆
     *
     * @param user    提交的用户数据，包含用户名和密码
     * @param session
     * @return
     */
    @PostMapping("/login")
    public Result login(@RequestBody User user, HttpSession session) {
        return userService.login(user,session);
    }

    /**
     * 获取用户名
     *
     * @param session
     * @return
     */
    @GetMapping("/getUser")
    public Result getUsername(HttpSession session) {
        return ResultUtil.ok(
                new UserVo()
                        .setUserId((String) session.getAttribute("userId"))
                        .setUserName((String) session.getAttribute("user"))
        );
    }
}
